package com.hsd.znsh.utils;


import com.hsd.znsh.enums.CodeEnum;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.enums.UserRoleEnum;

import java.util.*;

public class EnumUtil {

    public static <T extends CodeEnum> T getEnumByCode(Integer code, Class<T> enumClass) {
        for (T each: enumClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }

    public static <T extends CodeEnum> String getMessageByCode(Integer code, Class<T> enumClass) {
        T t=getEnumByCode(code,enumClass);
        if(t!=null) return t.getMessage();
        return null;
    }

    public static <T extends CodeEnum> List<Map<String,Object>>  getEnumInfo(Class<T> enumClass,String code) {
        List<Map<String,Object>> list=new ArrayList<>();
        for (T each: enumClass.getEnumConstants()) {
            Map<String,Object> map=new HashMap<>();
            map.put(code,each.getCode());
            map.put("typeName",each.getMessage());
            list.add(map);
        }
        return list;
    }

    public static <T extends CodeEnum> List<Map<String,Object>> getEnumInfo(Class<T> enumClass, String code, String message ) {
        List<Map<String,Object>> list=new ArrayList<>();
        for (T each: enumClass.getEnumConstants()) {
            Map<String,Object> map=new HashMap<>();
            map.put(code,each.getCode());
            map.put(message,each.getMessage());
            list.add(map);
        }
        return list;
    }

    public static void main(String[] args) {
        List<Map<String,Object>> list=EnumUtil.getEnumInfo(OnlineStatusEnum.class,"code","value");
        for(Map<String,Object> map:list){
            Set<String> strings=map.keySet();
            for(String s:strings){
                System.out.println(s+" : "+map.get(s));
            }
        }
    }

}
